#include "wifi_mqtt.h"

esp_mqtt_client_handle_t emcht;
bool mqtt_connect_flag = false;
extern int servo_value[6]; //舵机位置存储值
extern int step_on;
//WiFI事件处理函数
void  wifi_event_fun(void* handler_arg,esp_event_base_t event_base,int32_t event_id,void* event_data){
    printf("%s,%ld\r\n",event_base,event_id);
 
    if(event_id==WIFI_EVENT_STA_START){                 //如果是STA开启了,那么尝试连接
        esp_wifi_connect();                         
    }else if(event_id==WIFI_EVENT_STA_CONNECTED){       //连接上WiFI之后
        Mqtt_Init();                                  //开始连接MQTT服务器
    }else if(event_id==WIFI_EVENT_STA_DISCONNECTED){    //断开WiFi之后
        esp_wifi_connect();                             //尝试重连WiFi
    }
}
 
//MQTT事件处理函数
void mqtt_event_fun(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data){
    printf("%s,%ld\r\n",event_base,event_id);
 
    if(event_id==MQTT_EVENT_CONNECTED){                 //连接上MQTT服务器
        mqtt_connect_flag=true;
        esp_mqtt_client_subscribe_single(emcht,"XIAO_ESP32/LEDOUTPUT",0);    //订阅一个测试主题
        printf("success connect mqtt\r\n");
    }else if(event_id==MQTT_EVENT_DISCONNECTED){        //断开MQTT服务器连接
        mqtt_connect_flag=false;
        printf("lose connect mqtt\r\n");
    }else if(event_id==MQTT_EVENT_DATA){                //收到订阅信息
        esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t )event_data;   //转获取存放订阅信息的参数
        printf("receive data : %.*s from %.*s\r\n",event->data_len,event->data,event->topic_len,event->topic);
        //使用cJSON解析数据
        cJSON *root=cJSON_Parse(event->data);
        if(root){
            cJSON *unity_S1 = cJSON_GetObjectItem(root,"unity_S1");
            cJSON *unity_S2 = cJSON_GetObjectItem(root,"unity_S2");
            cJSON *unity_S3 = cJSON_GetObjectItem(root,"unity_S3");
            cJSON *unity_S4 = cJSON_GetObjectItem(root,"unity_S4");
            cJSON *unity_S5 = cJSON_GetObjectItem(root,"unity_S5");
            cJSON *is_Lock = cJSON_GetObjectItem(root,"is_Lock");
            cJSON *step_on_json = cJSON_GetObjectItem(root,"step_on");

            if(unity_S1){
                printf("unity_S1:%d\r\n",unity_S1->valueint);
                servo_value[1] = unity_S1->valueint;
                printf("servo_value[1]=%d\r\n",servo_value[1]);
            }
            if(unity_S2){
                printf("unity_S2:%d\r\n",unity_S2->valueint);
                servo_value[2] = unity_S2->valueint;
                printf("servo_value[2]=%d\r\n",servo_value[2]);
            }
            if(unity_S3){
                printf("unity_S3:%d\r\n",unity_S3->valueint);
                servo_value[3] = unity_S3->valueint;
                printf("servo_value[3]=%d\r\n",servo_value[3]);
            }
            if(unity_S4){
                printf("unity_S4:%d\r\n",unity_S4->valueint);
                servo_value[4] = unity_S4->valueint;
                printf("servo_value[4]=%d\r\n",servo_value[4]);

            }
            if(unity_S5){
                printf("unity_S5:%d\r\n",unity_S5->valueint);
                servo_value[5] = unity_S5->valueint;
                printf("servo_value[5]=%d\r\n",servo_value[5]);
            }
            if(is_Lock){
                printf("is_Lock:%d\r\n",is_Lock->valueint);
                servo_value[0]=is_Lock->valueint;
            }
            if(step_on_json){   
                step_on = step_on_json->valueint;
            }
            cJSON_Delete(root);
        }
    }
}




void WiFi_Init(void){
    nvs_flash_init();                           //初始化nvs
    esp_netif_init();                           //初始化TCP/IP堆栈
    esp_event_loop_create_default();            //创建默认事件循环
    esp_event_handler_register(WIFI_EVENT,ESP_EVENT_ANY_ID,wifi_event_fun,NULL);       //绑定事件处理函数
    esp_netif_create_default_wifi_sta();        //创建STA
    wifi_init_config_t wict = WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&wict);                       //初始化WiFI
    esp_wifi_set_mode(WIFI_MODE_STA);           //设为STA模式
    wifi_config_t wct = {
        .sta = {
            .ssid="bearpi",
            .password="12345678"
        }
    };
    esp_wifi_set_config(WIFI_IF_STA,&wct);      //设置WiFi
    esp_wifi_start();                           //启动WiFi
}

void Mqtt_Init(void){
    esp_mqtt_client_config_t emcct = {
        .broker.address.uri="mqtt://42.193.218.152",  //MQTT服务器的uri
        .broker.address.port=1883                   //MQTT服务器的端口
    };
    emcht = esp_mqtt_client_init(&emcct);           //初始化MQTT客户端获取句柄
    if(!emcht)  printf("mqtt init error!\r\n");
    
    //注册MQTT事件处理函数
    if(esp_mqtt_client_register_event(emcht,ESP_EVENT_ANY_ID,mqtt_event_fun,NULL)!=ESP_OK)  printf("mqtt register error!\r\n");
 
    //开启MQTT客户端
    if(esp_mqtt_client_start(emcht) != ESP_OK)  printf("mqtt start errpr!\r\n");
}
 
